<?php
namespace app\controller;

use app\BaseController;
use app\model\Brand as BrandModel;
use app\validate\Brand as BrandValidate;
use app\Request;
use think\exception\ValidateException;

class Brand extends BaseController{


    public function index(Request $request){
        $param = $request->param();
        $list = (new BrandModel)->getPageData($param['page'] = 1,10);
        return show(1,$list);
    }

    public function add(Request $request){
        try {
            $param = $request->param();

            $brand = new BrandModel();
            validate(BrandValidate::class)->scene('add')->check($param);
            $res =  $brand->save($param);
            if($res){
                return  show(1,'新增成功');
            }else{
                return  show(0,'新增失败');
            }
        }catch (\Exception $e){
            return show(0,$e->getMessage());
        }

    }

    public function edit(Request $request){
        try {
            $param = $request->param();

            $brand = new BrandModel();
            validate(BrandValidate::class)->scene('edit')->check($param);
            //$brand = $brand->find($param['id']);

 


               $res = $brand->where('id',$param['id'])->save($param);


                if($res){
                    return  show(1,'修改成功');
                }else{
                    return  show(0,'修改失败');
                }

        }catch (\Exception $e){
            return show(0,$e->getMessage());
        }
    }

    public function del(){
        $list = [
          'brand_name'=>'nameqwe123',
          'brand_img'=>'1231231',
            'create_time'=>null

        ];
        $brand = new BrandModel();
        $brand = $brand->find(2);

        foreach ($list as $key=>$value){
            $brand->$key = $value;
        }
        $brand->save();
        if($brand){
            return  show(1,'修改成功');
        }else{
            return  show(0,'修改失败');
        }
    }
}